﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using Base.Business;
using Base.Common;

namespace BaseApplication.Controllers
{
    public class RoleController : BaseController
    {
        //
        // GET: /Role/

        private RoleBLL roleBll = new RoleBLL();

        public ActionResult Index()
        {
            return View();
        }

        public JsonResult GetRole()
        {
            string search = Server.UrlDecode(Request["search"]);
            string pageSize = Request["PageSize"];
            string pageIndex = Request["PageIndex"];
            string sort = Request["Sort"];
            string sortField = string.Empty;
            string sortOrder = string.Empty;
            if (sort != null)
            {
                sortField = sort.Split('$')[0];
                sortOrder = sort.Split('$')[1];
            }
            DataSet ds = roleBll.GetRole(CurrentLanguage, int.Parse(pageSize), int.Parse(pageIndex) + 1, sortField, sortOrder == "1" ? "desc" : "asc", search);
            string json = ConvertHelper.GetJSON(ds);
            return Json(json, "application/json", JsonRequestBehavior.AllowGet);

        }

        public ActionResult Add()
        {
            return View();
        }

        public ActionResult Edit()
        {
            ViewData["Id"] = Request["Id"];
            DataTable dt = roleBll.GetRoleById(CurrentLanguage, ConvertHelper.GetGuid(Request["Id"]));
            if (dt.Rows.Count > 0)
            {
                ViewData["RoleName"] = dt.Rows[0]["RoleName"].ToString();
                ViewData["Description"] = dt.Rows[0]["Description"].ToString();
            }
            else
            {
                ViewData["RoleName"] = "";
                ViewData["Description"] = "";
            }
            return View();
        }

        public ActionResult Setting()
        {
            ViewData["Id"] = Request["RoleId"];
            DataTable dt = roleBll.GetRoleById(CurrentLanguage, ConvertHelper.GetGuid(ViewData["Id"]));
            if (dt.Rows.Count > 0)
            {
                ViewData["RoleName"] = dt.Rows[0]["RoleName"].ToString();
                ViewData["Description"] = dt.Rows[0]["Description"].ToString();
            }
            else
            {
                ViewData["RoleName"] = "";
                ViewData["Description"] = "";
            }
            return View();
        }


        [HttpPost]
        public ContentResult AddRole()
        {
            string roleName = Request["RoleName"];
            string description = Request["Description"];
            bool result = roleBll.AddRole(CurrentLanguage, roleName, description);
            return Content(result.ToString());
        }

        [HttpPost]
        public ContentResult UpdateRole()
        {
            Guid roleId = ConvertHelper.GetGuid(Request["Id"]);
            string roleName = Request["RoleName"];
            string description = Request["Description"];
            string MenuId = Request["MenuId"];

            List<string> listMenuId = MenuId.Split(';').ToList();
            List<Guid> listGuidMenuId = new List<Guid>();
            roleBll.DeleteRoleMenu(roleId);
            foreach (string sMenuId in listMenuId)
            {
                Guid GuidMenu = ConvertHelper.GetGuid(sMenuId);
                if (GuidMenu != Guid.Empty)
                {
                    roleBll.AddRoleMenu(roleId, ConvertHelper.GetGuid(sMenuId));
                }
            }
            bool result = roleBll.UpdateRole(CurrentLanguage, roleId, roleName, description);
            return Content(result.ToString());
        }

        [HttpPost]
        public ContentResult DeleteRole()
        {
            Guid roleId = ConvertHelper.GetGuid(Request["Id"]);

            bool result = roleBll.DeleteRole(roleId);
            return Content(result.ToString());
        }

        public JsonResult GetMenuRole()
        {
            Guid Id = ConvertHelper.GetGuid(Request["RoleId"]);
            //string Name = Request["Name"];
            //string LinkURL = Request["LinkURL"];
            //string Description = Request["Description"];
            //string MenuId = Request["MenuId"];
            //int IsDisplay = Convert.ToInt32(Request["IsDisplay"]);
            //int IsMenu = Convert.ToInt32(Request["IsMenu"]);
            //menuBll = new MenuBLL();
            //bool isSucc = menuBll.UpdateMenu(this.CurrentLanguage, Id, Name, LinkURL, Description, IsMenu, IsDisplay);
            DataTable dt = roleBll.GetMenuRole(CurrentLanguage, Id);
            string json = ConvertHelper.GetTreeJSON(dt);
            return Json(json, "application/json", JsonRequestBehavior.AllowGet);

        }

        public JsonResult GetUserRole()
        {
            Guid RoleId = ConvertHelper.GetGuid(Request["RoleId"]);
            DataTable dt = roleBll.GetUserRole(RoleId);
            string json = ConvertHelper.GetJSON(dt);
            return Json(json, "application/json", JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetUserNotInRole()
        {
            Guid RoleId = ConvertHelper.GetGuid(Request["RoleId"]);
            string employee = Request["search"].ToString();
            DataTable dt = roleBll.GetUserNotInRole(RoleId, employee);
            string json = ConvertHelper.GetJSON(dt);
            return Json(json, "application/json", JsonRequestBehavior.AllowGet);
        }

        public ContentResult AddUserToRole()
        {
            string ids = Request["id"];
            string[] idList = ids.Split(';'); 
            Guid RoleId = ConvertHelper.GetGuid(Request["roleId"]);
            for (int i = 0; i < idList.Length-1; i++)
            {
                bool isSuccess = roleBll.AddUserRole(ConvertHelper.GetGuid(idList[i]), RoleId);
            }

            return Content("True");
        }

        public ContentResult DeleteUserFromRole()
        {
            string ids = Request["id"];
            string[] idList = ids.Split(';');
            Guid RoleId = ConvertHelper.GetGuid(Request["roleId"]);
            for (int i = 0; i < idList.Length - 1; i++)
            {
                bool isSuccess = roleBll.DeleteUserRole(ConvertHelper.GetGuid(idList[i]), RoleId);
            }
            return Content("True");
        }
    }
}
